package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.ContentCatService;
import com.dongdongshop.service.ContentService;
import em.ResuleEnum;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    @DubboReference
    private ContentService contentService;
    @DubboReference
    private ContentCatService contentCatService;

    @RequestMapping("contentShow")
    public String contentShow(){
        return "admin/content";
    }

    @RequestMapping("contentList")
    @ResponseBody
    public AjaxResult contentList(){
        List<TbContent> list = contentService.selectInfo();
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(list);
    }

    @RequestMapping("toAdd")
    @ResponseBody
    public AjaxResult toAdd(){
        List<TbContentCategory> list = contentCatService.selectInfo(null);
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(list);
    }

    @RequestMapping("insertInfo")
    @ResponseBody
    public AjaxResult insertInfo(TbContent tbContent){
        contentService.insertInfo(tbContent);
        return AjaxResult.R(ResuleEnum.SUCCESS);
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public AjaxResult toUpdate(Long id){
        List<TbContentCategory>list = contentCatService.selectInfo(null);
        TbContent tbContent = contentService.toUpdate(id);
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(tbContent).setRows(list);
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public AjaxResult updateInfo(TbContent tbContent){
        contentService.updateInfo(tbContent);
        return AjaxResult.R(ResuleEnum.SUCCESS);
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public AjaxResult deleteById(@RequestParam("ids[]")Long[] ids){
        contentService.deleteById(ids);
        return AjaxResult.R(ResuleEnum.SUCCESS);
    }
}
